package com.stylefeng.guns.rest.modular.wz.api.v3;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.persistence.dao.WzUserAdvMapper;
import com.stylefeng.guns.rest.common.persistence.dao.WzUserAdvRecordMapper;
import com.stylefeng.guns.rest.common.persistence.model.WzAdv;
import com.stylefeng.guns.rest.common.persistence.model.WzUser;
import com.stylefeng.guns.rest.common.persistence.model.WzUserAdv;
import com.stylefeng.guns.rest.common.persistence.model.WzUserAdvRecord;
import com.stylefeng.guns.rest.common.util.StringUtils;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.EnWzAdv;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.controller.WzFinanceService;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzAdvServiceImpl;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author ruiyifeng123
 * @since 2018-03-20
 */
@RestController
@RequestMapping("/wz/api/v3/adv/")
public class WzAdvApiV3 extends ApiBaseController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private WzAdvServiceImpl advService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzUserAdvRecordMapper userAdvRecordMapper;
    @Autowired
    private WzFinanceService financeService;
    @Autowired
    private WzUserAdvMapper userAdvMapper;



    /**
     * 广告领钱
     */
    @PostMapping("advReward")
    public Res advReward(@RequestBody ParamMap paramMap){
        String androidVersion = paramMap.getStrValid("androidVersion");
        String iosVersion = paramMap.getStrValid("iosVersion");
        String adCode = paramMap.getStrValid("adCode");
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        boolean rr = true;
        int ios = 0;
        int android = 0;
        if(!StringUtils.isBlankStr(iosVersion)){
            ios = Integer.parseInt(iosVersion.substring(0,1))*100 + Integer.parseInt(iosVersion.substring(2,3))*10 + Integer.parseInt(iosVersion.substring(4,5));
            if(ios >= Integer.parseInt(Consts.iosVersion)){
                rr = false;
            }
        }
        if(!StringUtils.isBlankStr(androidVersion)){
            android = Integer.parseInt(androidVersion);
            if(android >= Integer.parseInt(Consts.androidVersion)){
                rr = false;
            }
        }
        if(rr){
            return Res.fail("版本太低无法领钱，请更新!");
        }
        //位置判断
        if(adCode.length() != 6){
            return Res.fail(BizExceptionEnum.RES_Ad_Code_Abnormal);
        }
        Long advId = paramMap.getLongMisValid("advId");
        WzAdv adv = advService.selectById(advId);
        boolean f = advService.adCodeCheck(adCode,adv.getScreenCity());
        if(!f){
            return Res.fail("所在地区无法领钱!");
        }

        WzUser user = cacheService.user(userCode);
        List<WzUserAdvRecord> record=userAdvRecordMapper.selectList(new EntityWrapper<WzUserAdvRecord>().eq("user_code",user.getUserCode()).eq("adv_id",adv.getAdvId()));
        if(record.size()>0){
            return new Res(BizExceptionEnum.ADV_SHIXIAO.getCode(), BizExceptionEnum.ADV_SHIXIAO.getMsg());
        }
        adv.setPushNumber(adv.getPushNumber() + 1);
        //发布人数已满
        if(adv.getPushNumber() >= adv.getTotalNumber()){
            adv.setStatus(EnWzAdv.STATUS_RELEASE_yes.getIndex());
        }

        //领钱金额
//        int rewardAverage = adv.getRewardAverage();
//        boolean r = financeService.advReward(adv,user);
        List<WzUserAdv> userAdvs = userAdvMapper.selectList(new EntityWrapper<WzUserAdv>().eq("adv_id", adv.getAdvId().toString()).eq("sort", adv.getPushNumber()));
        //随机金额处理
//        int rewardAverage = userAdvs.get(0).getReward();

        Date releaseTime=adv.getReleaseTime();
        int rewardAverage;
        if(releaseTime.before(DateUtil.toDate("2019-4-3"))){
            rewardAverage=adv.getRewardAverage();
        }else{
            //随机金额处理
            rewardAverage= userAdvs.get(0).getReward();
        }

        boolean r = financeService.advReward(adv,user,rewardAverage);

        if(r){
            paramMap.clear();
            paramMap.put("reward",rewardAverage);
            return Res.success(paramMap);
        }
        return Res.fail("请重新领取");
    }

}

